using DG.Tweening;
using PP.Basic;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace PP.PPPoker.Game.GJ25
{
    /// <summary>
    /// GJCard 对象
    /// </summary>
    public class GJCard : MonoBehaviour
    {
        private const string Tag = nameof(GJCard);
        private const string UpAnimationName = "CardUp";
        private const string DownAnimationName = "CardDown";

        // Private Variables Begin
        private int _cardCode = 0;
        private int _cardIndex = 0;

        private AnimationTrigger AnimationTrigger => _animationTrigger ?? (_animationTrigger = transform.GetComponentEx<AnimationTrigger>());
        private AnimationTrigger _animationTrigger;

        private bool _isSeleced = false;
        public bool IsSelected => _isSeleced;
        public int Code => _cardCode;
        // Private Variables End
        
        // Static Methods Begin
        public static GJCard Create(Transform parent = null)
        {
            return null;
        }
        // Static Methods End

        // Public Methods Begin

        public void SetCardCode(int cardCode)
        {
            _cardCode = cardCode;

            Debug.Log($"SetCardCode : cardCode={cardCode},path={PokerCardConfig.GetPokerCardImagePath(cardCode)} ");
            _cardImage.sprite = Resources.Load<Sprite>(PokerCardConfig.GetPokerCardImagePath(cardCode));
            _cardImage.gameObject.SetActive(true);
        }

        public void Reset()
        {
            _cardCode = 0;
            _isSeleced = false;
            AnimationTrigger.PauseAnimation(DownAnimationName, 1);
            _cardImage.sprite = null;
            _cardImage.gameObject.SetActive(false);
        }




        public void PlayUp(Action onComplete = null)
        {
            AnimationTrigger.Play(UpAnimationName, onComplete);
            _isSeleced = true;
        }

        public void PlayDown(Action onComplete = null)
        {
            AnimationTrigger.Play(DownAnimationName, onComplete);
            _isSeleced = false;
        }



        public void PlayRemove(Action onComplete = null)
        {
            AnimationTrigger.Play("CardFadeOut", () =>
            {
                Reset();
                onComplete?.Invoke();
            });
        }


        public void PlayDealIn(Action onComplete = null)
        {
            AnimationTrigger.Play("CardFadeIn", () =>
            {
                onComplete?.Invoke();
            });
        }




        // Public Methods End

        // Lifecycle Methods Begin
        protected void Awake()
        {
            InitHier();
            InitPart();
        }



        private void InitPart()
        {
            // Do initialization here...
        }
        
        private void Start()
        {
            // Debug.Log($"{Tag}.Start");
        }
   
        // Lifecycle Methods End


        // Hier Components Begin        
        private Image _cardImage;
        private Button _button;

        private void InitHier()
        {
            _cardImage = transform.Find("Content/_Image_Card").GetComponent<Image>();
            _button = transform.Find("Content/_Button_").GetComponent<Button>();
            _button.onClick.AddListener(OnButtonClick);
        }


        private void OnButtonClick()
        {
            Debug.Log($"{Tag}.OnButtonClick {_cardCode}");
            if (_cardCode <= 0)
                return;

            if (_isSeleced)
            {
                PlayDown(() =>
                {
                    Debug.Log($"{Tag}.OnButtonClick Down {_cardCode}");
                });
            }
            else if (!_isSeleced)
            {
                PlayUp(() =>
                {
                    Debug.Log($"{Tag}.OnButtonClick Up {_cardCode}");
                });
            }
        }
        // Hier Components End
    }
}
